package com.song.osclient.fragment.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.song.osclient.R;
import com.song.osclient.bean.Page;
import com.song.osclient.view.CustomViewPageStrip;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;


public abstract class BaseViewPagerFragment extends Fragment {
    @InjectView(R.id.viewPager)
    ViewPager viewPager;
    @InjectView(R.id.strip)
    CustomViewPageStrip strip;
    FragmentPagerAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_base_view_page, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        ButterKnife.inject(this, view);
        initViewPager();
    }


    private void initViewPager() {
        final List<Page> pageInfo = getPageInfo();
        strip.bindViewPagerWithTitleData(viewPager, pageInfo);
        adapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return pageInfo.get(position).getFragment();
            }

            @Override
            public int getCount() {
                return pageInfo.size();
            }
        };
        viewPager.setAdapter(adapter);
    }

    public abstract List<Page> getPageInfo();
}
